%   Main source code file
%
%   CASE_ASPETE_MV Power flow data for a 100 nodes distribution line.
%
%   Based on data of the Hellenic Distribution system

%   MATPOWER

%   MC is the random number generator
%   Iterations is the total number of iterations
%   Optimisation formula (loads active power (total: 4122kW), PV power (total: 6929), Voltage minimum)

%----------------------------------
% initiation
clear
OF = 0;
OFR= 0;
PV = 0.02;

for t = 1:1:4*10^6

MC = rand(45,1);

results = runpf(case_aspete_MV_monte_carlo_RES(MC,PV));
bus = min(results.bus);

AP1 = 0.017*MC(1) + 0.164*MC(2) + 0.243*MC(3) + 0.504*MC(4) + 0.034*MC(5) + 0.122*MC(6) + 0.017*MC(7);
AP2 = 0.034*MC(8) + 0.274*MC(9) + 0.034*MC(10) + 0.108*MC(11) + 0.139*MC(12);
AP3 = 0.311*MC(13)+ 0.025*MC(14)+ 0.189*MC(15) +0.085*MC(16) +0.059*MC(17) +0.085*MC(18) +0.139*MC(19);
AP4 = 0.034*MC(20)+ 0.034*MC(21)+ 0.054*MC(22) +0.017*MC(23) +0.068*MC(24) +0.184*MC(25) +0.034*MC(26);
AP5 = 0.071*MC(27)+ 0.034*MC(28)+ 0.017*MC(29) +0.054*MC(30);
AP6 = 0.051*MC(31)+ 0.034*MC(32)+ 0.054*MC(33) +0.223*MC(34)+ 0.054*MC(35);
AP7 = 0.085*MC(36) +0.025*MC(37)+ 0.034*MC(38);
AP8 = 0.085*MC(39)+ 0.017*MC(40)+ 0.054*MC(41) +0.085*MC(42)+ 0.034*MC(43)+ 0.054*MC(44) +0.054*MC(45);
AP = AP1 + AP2 + AP3 + AP4 + AP5 + AP6 + AP7 + AP8;

% OF is 0.4 times of pu total load and 0.2 times of minimum pu voltage.
OF  = 0.8*AP/4.122 + 0.2*bus(8);

if OFR<OF
OFR = OF;
MCR2 = MC;
end

OF2 = OFR;
end

OF2
MCR2
